
dofile("Secretary.lua")

function dump(name, value, o)
	local format = string.format
	local dumped = {}
	
	local print = print
	if o then
		print = function (s)
			o:write(s.."\n")
		end
	end
	
	
	local toliteral = function(key)
		local t = type(key)
		if t == "string" then
			return format("%q", key)
		else
			return tostring(key)
		end
	end
	
	local toliteralkey = function (dumped, key)
		local tk = type(key)
		if tk == "string" then
			if key:len() > 0 and not key:find("[^%a%d_]") and not key:sub(1, 1):find("%d") then
				return key
			else
				return format("[%q]", key)
			end
		elseif tk == "table" then
			if dumped[key] then
				return "["..dumped[key].."]"
			end
		end
		return "["..toliteral(key).."]"
	end
	
	local function realdump(name, rname, value, offset)
		local e = offset == "" and "" or ","
		local keyname = toliteralkey(dumped, name)
		if type(value) == "table" then
			if dumped[value] then
				print(format("%s%s = %s%s", offset, keyname, dumped[value], e))
			else
				dumped[value] = rname
				print(format("%s%s = {", offset, keyname))
				if keyname:sub(1, 1) ~= "[" then
					keyname = "."..keyname
				end
				local roffset = offset.."  "
				local index = 1
				for key, val in pairs(value) do
					local k = toliteralkey(dumped, key)
					if k:sub(1, 1) ~= "[" then
						k = "."..k
					end
					realdump(key, rname..k, val, roffset)
				end
				print(format("%s}%s", offset, e))
			end
		else
				print(format("%s%s = %s%s", offset, keyname, toliteral(value), e))
		end
	end
	
	realdump(name, toliteralkey(nil, name), value, "")
end

function cleaneventsUNKNOWN()
	local i = 1
	local events = SecretaryDB.profiles.Default.events
	local player_events = {
		enter = true,
		leave = true,
		afk = true,
		offline = true,
		["return"] = true,
	}
	local remove = table.remove
	local UNKNOWN = "Inconnu"
	
	while i <= #events do
		local event = events[i]
		if player_events[event.type] and event.player == UNKNOWN then
			remove(events, i)
		else
			i = i + 1
		end
	end
	
	SecretaryDB.profiles.Default.chars[UNKNOWN] = nil
end

--~ cleaneventsUNKNOWN()

--~ dump("SecretaryDB", SecretaryDB)